const app = getApp()
Page({
  data: {
    navShow: false,
    type: [
      // {title:'精品手机',img:''},
      // {title:'爆款电脑',img:''},
      // {title:'全新平板',img:''},
      // {title:'耳机设备',img:''},
      // {title:'手表饰品',img:''},
    ],
    cate: [
      // {title:'小米1',img:''},
      // {title:'小米2',img:''},
      // {title:'小米3',img:''},
      // {title:'小米4',img:''},
      // {title:'小米5',img:''},
      // {title:'小米6',img:''},
      // {title:'小米7',img:''},
      // {title:'小米8',img:''},
      // {title:'小米9',img:''},
      // {title:'小米10',img:''},
    ],
    typeIndex: 0, // 分类索引
    cateIndex: 0, // 品牌索引
    indicatorIndex: 0, //指示点
    scrollTop: '', // 顶部距离

    search: {
      filterWord: '',
      rows: 10,
      page: 1,
      tid: null,
    },
    pruduct: [],
    totalResult: '', // 数量


    show: false, // 显示加载
    upisd: '',
    hd: false,
    toView: '',
    scrollleft: '300',
    qhid: '2',
    valuesj: '店铺',
    showsjiao: false,
    systemInfo: {},
    up: true,
    ups: true,
    inputwidth: 220,
    inputboxwidth: 320,
    navShow: false,
    type: [{
        title: '精品手机',
        img: ''
      },
      {
        title: '爆款电脑',
        img: ''
      },
      {
        title: '全新平板',
        img: ''
      },
      {
        title: '耳机设备',
        img: ''
      },
      {
        title: '手表饰品',
        img: ''
      },
    ],
    cate: [{
        title: '小米1',
        img: ''
      },
      {
        title: '小米2',
        img: ''
      },
      {
        title: '小米3',
        img: ''
      },
      {
        title: '小米4',
        img: ''
      },
      {
        title: '小米5',
        img: ''
      },
      {
        title: '小米6',
        img: ''
      },
      {
        title: '小米7',
        img: ''
      },
      {
        title: '小米8',
        img: ''
      },
      {
        title: '小米9',
        img: ''
      },
      {
        title: '小米10',
        img: ''
      },
    ],
    typeIndex: 0, // 分类索引
    cateIndex: 0, // 品牌索引
    indicatorIndex: 0, //指示点
    scrollTop: '', // 顶部距离

    search: {
      tit: '',
      filterWord: '',
      pageSize: 10,
      pageNo: 1,
      tid: null,
    },
    pruduct: [],
    totalCount: '', // 数量

  },
  onLoad() {
    app._init_(this)
    this.getSystemInfo()
    this.getType()
    app.editTabbar();
    if (options.id) {
      app.globalData.categorynum = options.id
    }

  },
  onShow() {
    if (app.globalData.categorynum == '') {
      this.setData({
        typeIndex: 0,
        inputwidth: 220,
        inputboxwidth: 320,
        toView: 'xx' + 0,
      })
      this.getType(0)
    } else {
      let nummm = app.globalData.categorynum - 1;
      this.setData({
        typeIndex: app.globalData.categorynum - 1,
        inputwidth: 220,
        inputboxwidth: 320,
      })
      this.getType(app.globalData.categorynum - 1)
    }
    this.homeToCate()
  },
  getSystemInfo() {
    my.getSystemInfo({
      success: (res) => {
        console.log("444", res)
        this.setData({
          systemInfo: res
        })
      }
    })
  },
  homeToCate() {
    console.log(app.globalData.catePage_typeIndex);
    const appIndex = app.globalData.catePage_typeIndex
    const typeIndex = this.data.typeIndex
    console.log(appIndex, typeIndex)
    if (appIndex && appIndex !== typeIndex) {
      let search = this.data.search;
      search.filterWord = '';
      search.rows = 10;
      search.page = 1;
      // search.tid = null;
      console.log(this.data.type.filter(item => item.id == appIndex)[0].childrenList.reverse())
      const cate = this.data.type.filter(item => item.id == appIndex)[0].childrenList.reverse();
      this.setData({
        search: search,
        typeIndex: appIndex,
        cateIndex: 0,
        cate: cate,
        pruduct: []
      })
      this.setSearch()
    }
  },


  getType() {
    app.other.type()
      .then(res => {
        console.log("============>.", res)
        let type = res.data
        if (type.length == 0) return;
        type = type.reverse()
        const id = type[0].id
        let cate = type.filter(item => item.id == id)[0].childrenList.reverse();
        let search = this.data.search;
        search.tid = cate[0].id;
        this.setData({
          cate,
          search,
          type,
          typeIndex: id
        })
        this.submit()
      })
  },
  // 分类点击
  typeTap(e) {
    console.log(e.target.dataset.index)
    let search = this.data.search;
    search.filterWord = '';
    search.rows = 10;
    search.page = 1;
    // search.tid = null;
    const index = e.target.dataset.index;
    console.log(this.data.type.filter(item => item.id == index)[0].childrenList.reverse())
    const cate = this.data.type.filter(item => item.id == index)[0].childrenList.reverse();
    this.setData({
      search: search,
      typeIndex: index,
      cateIndex: 0,
      cate: cate,
      pruduct: []
    })
    this.setSearch()
  },
  // 分类点击
  handleChange(index) {
    let search = this.data.search;
    search.filterWord = '';
    search.rows = 10;
    search.page = 1;
    search.tid = null;
    this.setData({
      search: search,
      cateIndex: index,
      pruduct: []
    });
    this.setSearch()
  },
  onChange(index) {
    this.setData({
      cateIndex: index,
    });
  },

  // 设置搜索参数
  setSearch() {
    const search = this.data.search,
      cateIndex = this.data.cateIndex,
      cate = this.data.cate;
    search.rows = 10;
    search.page = 1;
    search.tid = cate[cateIndex].id;

    this.setData({
      search: search
    })
    this.submit()
  },


  // 跳转
  navigateTo(e) {
    app.navigateTo(e)
  },
  // 输入完成
  onConfirm(e) {
    let search = this.data.search;
    search.filterWord = e.detail.value;
    search.rows = 10;
    search.page = 1;
    search.tid = null;
    this.setData({
      search,
      pruduct: []
    })
    this.submit()
  },



  // 搜索
  submit() {
    app.product.page(this.data.search)
      .then(res => {
        console.log(res)
        let totalResult = res.data.totalResult
        this.dataPromise(res.data.list)
          .then(res => {
            let pruduct = [...this.data.pruduct, ...res]
            this.setData({
              pruduct,
              totalResult
            })
          })
      })
  },
  // 转价格
  dataPromise(list) {
    return new Promise((relose, reject) => {
      list.forEach((item) => {
        item.category = JSON.parse(item.category)
        item.colors = JSON.parse(item.colors)
        item.match = JSON.parse(item.match)
        item.params = JSON.parse(item.params)
        item.slideshows = JSON.parse(item.slideshows)
        this.getminPrice(item.params)
          .then(res => {
            item.price = res
          })
      })
      relose(list)
    })
  },

  getminPrice(params) {
    return new Promise((resole, reject) => {
      let prices = []
      for (let i = 0; i < params.length; i++) {
        for (let t = 0; t < params[i].term_list.length; t++) {
          for (let s = 0; s < params[i].term_list[t].specifications.length; s++) {
            let price = (params[i].term_list[t].specifications[s].money / params[i].term_list[t].value / 100).toFixed(2)
            prices.push(price)
          }
        }
      }
      let minprice = Math.min.apply(null, prices);
      resole(minprice)
    })
  },

  // 下拉刷新
  onPullDownRefresh() {
    let e = {
      detail: {
        value: ''
      }
    }
    this.onConfirm(e)
    setTimeout(() => {
      my.stopPullDownRefresh({
        complete(res) {}
      })
    }, 1000)
  },
  stopPullDownRefresh() {
    my.stopPullDownRefresh({
      complete(res) {
        // console.log(res, new Date())
      }
    })
  },
  //   下拉加载
  onReachBottom() {
    let search = this.data.search,
      pruduct = this.data.pruduct,
      totalResult = this.data.totalResult;
    try {
      // 判断是否还有数据需要加载
      if (pruduct.length < totalResult) {
        search.page++
        this.setData({
          search
        })
        this.submit();
      }
    } catch (e) {}
  },
});